package com.chenyitun.cloudadmin.common.controller;

import com.bootdo.common.domain.DictType;
import com.bootdo.common.service.DictTypeService;
import com.bootdo.common.utils.bootdo.PageUtils;
import com.bootdo.common.utils.bootdo.Query;
import com.bootdo.common.utils.bootdo.R;
import com.bootdo.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 字典类型表
 *
 * @author admin
 * @email chen@163.com
 * @date 2019-02-15 09:16:13
 */

@Controller
@RequestMapping("/common/dictType")
public class DictTypeController extends BaseController {
    @Autowired
    private DictTypeService dictTypeService;

    @GetMapping()
    @RequiresPermissions("common:dictType:dictType")
    String DictType() {
        return "common/dictType/dictType";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("common:dictType:dictType")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<DictType> dictTypeList = dictTypeService.list(query);
        int total = dictTypeService.count(query);
        PageUtils pageUtils = new PageUtils(dictTypeList, total);
        return pageUtils;
    }


    @GetMapping("/add")
    @RequiresPermissions("common:dictType:add")
    String add() {
        return "common/dictType/add";
    }


    @GetMapping("/edit/{id}")
    @RequiresPermissions("common:dictType:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        DictType dictType = dictTypeService.get(id);
        model.addAttribute("dictType", dictType);
        return "common/dictType/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("common:dictType:add")
    public R save(DictType dictType) {
        if (!beanValidator(dictType)) {
            return R.error();
        }
        if (dictTypeService.save(dictType) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("common:dictType:edit")
    public R update(DictType dictType) {
        dictTypeService.update(dictType);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("common:dictType:remove")
    public R remove(Long id) {
        if (dictTypeService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("common:dictType:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        dictTypeService.batchRemove(ids);
        return R.ok();
    }

    /**
     * 导出列表
     */
    @RequiresPermissions("common:dictType:export")
    @PostMapping("/export")
    @ResponseBody
    public R export(@RequestParam Map<String, Object> params) {
        List<DictType> list = dictTypeService.list(params);
        ExcelUtil<DictType> util = new ExcelUtil<DictType>(DictType.class);
        return util.exportExcel(list, "dictType");
    }

    /**
     * 查询字典详细
     */
    @RequiresPermissions("common:dictType:dictType")
    @GetMapping("/detail/{dictId}")
    public String detail(@PathVariable("dictId") Long dictId, Model model) {
        model.addAttribute("dict", dictTypeService.get(dictId));
        model.addAttribute("dictList", dictTypeService.alllist());
        return "common/dictData/dictData";
    }

    /**
     * 校验字典类型
     */
    @PostMapping("/checkDictTypeUnique")
    @ResponseBody
    public String checkDictTypeUnique(DictType dictType) {
        return dictTypeService.checkDictTypeUnique(dictType);
    }


}
